package JDBC;

import java.sql.*;

/**
 * @Author: ylc
 * @Description:通过JDBC进行DQL操作
 * @Date Created in 2022-05-09 20:42
 */
public class JDBCDemo2 {
    public static void main(String[] args) {
        Connection conn = null;
        Statement stmt = null;
        ResultSet rs = null;
        try {
            Class.forName("com.mysql.jdbc.Driver");
            conn = DriverManager.getConnection("jdbc:mysql:///study","root","root");
            stmt = conn.createStatement();
            String sql = "select * from job";
            //查询结果封装在结果集对象中
            rs = stmt.executeQuery(sql);
            while(rs.next()){   //判断游标所在行是否有数据
                //获取数据
                //参数可以为列所在的下标（从1开始），也可以是列的名称
                int id = rs.getInt("id");
                String jname = rs.getString(2);
                String description = rs.getString(3);

                System.out.println(id + "---" + jname + "---" + description);
            }

        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }catch (SQLException throwables) {
            throwables.printStackTrace();
        }finally {
            try {
                rs.close();
            } catch (SQLException throwables) {
                throwables.printStackTrace();
            }
            try {
                stmt.close();
            } catch (SQLException throwables) {
                throwables.printStackTrace();
            }
            try {
                conn.close();
            } catch (SQLException throwables) {
                throwables.printStackTrace();
            }
        }
    }
}
